package routers

import (
	"encoding/json"
	"net/http"
	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/entity"
	"strconv"

	"github.com/jinzhu/gorm"

	"github.com/labstack/echo"
)

//对公客户企业类型维护添加主客户
func CompClieKhglInsCRouter(c echo.Context) error {
	cuid := c.FormValue("CUID")
	tx := base.DB.Begin()
	id, err := strconv.Atoi(cuid)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	sqlDivi := "SELECT * FROM T_COMP_CLIE_KHGL WHERE CUID =?"
	rows, err := base.DB.Raw(sqlDivi, id).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	for rows.Next() {
		tx.Rollback()
		return c.JSON(http.StatusOK, "NO")
	}
	tx.Exec("insert into T_COMP_CLIE_KHGL (CUID) VALUES (?)", id)

	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//对公关联客户维护添加关联客户
func CompClieKhglInsGlRouter(c echo.Context) error {
	cuid := c.FormValue("CUID")
	jsonStr := c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.CompClieKhgl
	err := json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	var CompClieKhgl []entity.CompClieKhgl
	tx := base.DB.Begin()
	id, err := strconv.Atoi(cuid)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	//删除关联客户为null
	err = base.DB.Where("CUID = ? AND LCUID IS NULL", id).Delete(&CompClieKhgl).Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "删除关联客户为null失败！")
	}
	//新增关联客户
	for _, data := range dataSource {
		newData := entity.CompClieKhgl{
			CUID:  data.CUID,
			LCUID: data.LCUID,
		}
		err = base.DB.Create(&newData).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//中小客户企业类型维护添加主客户
func CompClieKhglZxInsCRouter(c echo.Context) error {
	cuid := c.FormValue("CUID")
	tx := base.DB.Begin()
	id, err := strconv.Atoi(cuid)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	sqlDivi := "SELECT * FROM T_COMP_CLIE_KHGL_LS WHERE CUID =?"
	rows, err := base.DB.Raw(sqlDivi, id).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	for rows.Next() {
		tx.Rollback()
		return c.JSON(http.StatusOK, "NO")
	}
	tx.Exec("insert into T_COMP_CLIE_KHGL_LS (CUID) VALUES (?)", id)

	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//中小关联客户维护添加关联客户(1对公/2零售)
func CompClieKhgZxlInsGlRouter(c echo.Context) error {
	cuid := c.FormValue("CUID")
	jsonStr := c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.CompClieKhglLs
	err := json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	var CompClieKhgl []entity.CompClieKhglLs
	tx := base.DB.Begin()
	id, err := strconv.Atoi(cuid)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	//删除关联客户为null
	err = base.DB.Where("CUID = ? AND LCUID IS NULL", id).Delete(&CompClieKhgl).Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "删除关联客户为null失败！")
	}
	//新增关联客户
	for _, data := range dataSource {
		newData := entity.CompClieKhglLs{
			CUID:  data.CUID,
			LCUID: data.LCUID,
			KHLX:  data.KHLX,
		}
		err = base.DB.Create(&newData).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//对公客户综合目标设置
func CompClieZhsymbInsRouter(c echo.Context) error {
	cuid := c.FormValue("CUID")
	jsonStr := c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var CompClieZhsrmb entity.CompClieZhsrmb
	err := json.Unmarshal([]byte(jsonStr), &CompClieZhsrmb)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx := base.DB.Begin()
	id, err := strconv.Atoi(cuid)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	var count int
	err = tx.Where("CUID=? ", cuid).Find(&CompClieZhsrmb).Count(&count).Error
	if err != nil && err == gorm.ErrRecordNotFound { //找不到数据行处理方法
		count = 0
	} else {
		return err
	}
	if count > 0 {
		return c.JSON(http.StatusOK, "NO")
	}
	//新增关联客户
	newData := entity.CompClieZhsrmb{
		CUID:  id,
		SDATE: CompClieZhsrmb.SDATE,
		EDATE: CompClieZhsrmb.EDATE,
		CKSD:  CompClieZhsrmb.CKSD,
		CKRJ:  CompClieZhsrmb.CKRJ,
		MNLR:  CompClieZhsrmb.MNLR,
		ZSSR:  CompClieZhsrmb.ZSSR,
	}
	err = base.DB.Create(&newData).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//中小客户综合目标设置
func CompClieZhsyMbsdInsRouter(c echo.Context) error {
	cuid := c.FormValue("CUID")
	jsonStr := c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var datasource entity.CompClieZhsyMbsd
	err := json.Unmarshal([]byte(jsonStr), &datasource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx := base.DB.Begin()
	id, err := strconv.Atoi(cuid)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	var count int
	err = tx.Where("CUID=? ", cuid).Find(&datasource).Count(&count).Error
	if err != nil && err == gorm.ErrRecordNotFound { //找不到数据行处理方法
		count = 0
	} else {
		return err
	}
	if count > 0 {
		return c.JSON(http.StatusOK, "NO")
	}
	//新增关联客户
	newData := entity.CompClieZhsyMbsd{
		CUID:  id,
		SDATE: datasource.SDATE,
		EDATE: datasource.EDATE,
		CKSD:  datasource.CKSD,
		CKRJ:  datasource.CKRJ,
		MNLR:  datasource.MNLR,
		ZSSR:  datasource.ZSSR,
	}
	err = base.DB.Create(&newData).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//DelCompMainClieRouter 对公客户企业维护删除主客户
func DelCompMainClieRouter(c echo.Context) error {
	id := c.FormValue("ids")
	tx := base.DB.Begin()
	var CompClieKhgl []entity.CompClieKhgl
	var count int
	err := tx.Where("CUID=? AND LCUID IS NOT NULL", id).Find(&CompClieKhgl).Count(&count).Error
	if err != nil {
		return err
	}
	if count == 0 {
		err = tx.Exec("DELETE FROM T_COMP_CLIE_KHGL WHERE CUID=?", id).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
		}
		//删除客户收益
		err = tx.Exec("DELETE FROM T_COMP_CLIE_ZHSYMB WHERE CUID=?", id).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
		}
	}
	tx.Commit()
	if count == 0 {
		return c.JSON(http.StatusOK, "OK")
	} else {
		return c.JSON(http.StatusOK, "no")
	}
}

//DelCompOtherClieRouter 对公客户企业维护删除关联客户
func DelCompOtherClieRouter(c echo.Context) error {
	mkeys := c.FormValue("mkeys")
	okeys := c.FormValue("okeys")
	tx := base.DB.Begin()
	err := tx.Exec("UPDATE T_COMP_CLIE_KHGL SET LCUID=NULL WHERE CUID=? AND LCUID=? ", mkeys, okeys).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//delCompClieMainRouter 对公中小客户企业维护删除主客户
func delCompClieMainRouter(c echo.Context) error {
	id := c.FormValue("cuid")
	tx := base.DB.Begin()
	var CompClieKhglLs []entity.CompClieKhglLs
	var count int
	err := tx.Where("CUID=? AND LCUID IS NOT NULL", id).Find(&CompClieKhglLs).Count(&count).Error
	if err != nil {
		return err
	}
	if count == 0 {
		err = tx.Exec("DELETE FROM T_COMP_CLIE_KHGL_LS WHERE CUID=?", id).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
		}
	}
	tx.Commit()
	if count == 0 {
		return c.JSON(http.StatusOK, "OK")
	} else {
		return c.JSON(http.StatusOK, "no")
	}
}

//DelCompClieGlRouter 中小对公客户关联
func DelCompClieGlRouter(c echo.Context) error {
	mkeys := c.FormValue("mkeys")
	dgkeys := c.FormValue("dgkeys")
	tx := base.DB.Begin()
	err := tx.Exec("UPDATE T_COMP_CLIE_KHGL_LS SET LCUID=NULL,KHLX=NULL WHERE CUID=? AND LCUID=? AND KHLX=1", mkeys, dgkeys).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//DelIndiClieGlRouter 中小零售客户关联
func DelIndiClieGlRouter(c echo.Context) error {
	mkeys := c.FormValue("mkeys")
	lskeys := c.FormValue("lskeys")
	tx := base.DB.Begin()
	err := tx.Exec("UPDATE T_COMP_CLIE_KHGL_LS SET LCUID=NULL,KHLX=NULL WHERE CUID=? AND LCUID=? AND KHLX=2", mkeys, lskeys).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//delSymbRouter 对公收益目标删除
func delSymbRouter(c echo.Context) error {
	otherkey := c.FormValue("otherkey")
	tx := base.DB.Begin()
	err := tx.Exec("DELETE FROM T_COMP_CLIE_ZHSYMB WHERE ID=?", otherkey).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

//delSymbzxRouter 中小收益目标删除
func delSymbzxRouter(c echo.Context) error {
	otherkey := c.FormValue("otherkey")
	tx := base.DB.Begin()
	err := tx.Exec("DELETE FROM T_COMP_CLIE_ZHSY_MBSD WHERE ID=?", otherkey).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, "删除失败！")
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}
